#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <sys/time.h>

#define SPORT  41501
#define MAXLOG 1
#define MAX_PACKET 120

void usage()
{
    printf("\n");
    printf("description: the hplog client controller\n");
    printf("usage: command {srv addr} action                parameter\n");
    printf("                          1 color               0 disable color \n");
    printf("                                                1 enable color, color string is must\n");
    printf("                          3 daemon_log_level    value of the level\n");
    printf("                          7 update the dest\n");
    printf("                  pid     8 user app log_level  value of the level(this command can only be set in the local PC)\n\n");
    printf("For example:\n");
    printf("clientctl 10.1.136.8 1 0\n  disable the color functionality\n");
    printf("clientctl 10.1.136.8 1 1 \"999[1-2]8\"\n  enable the color with the string\n");
    printf("clientctl 10.1.136.8 3 4\n  set the daemon log_level to 4 (0-7)\n");
    printf("clientctl 10.1.136.8 7 \n  update the destination of the client log daemon\n");
    printf("clientctl 1066 8 6\n  set the user's app(1066) log_level to 6(0-7)\n\n");
}

int main(int argc, char *argv[])
{
    int sockfd,idebug;
    long long counter,ipos; 
    //struct hostent *host;
    union sigval mysigval;
    struct sockaddr_in serv_addr;
    struct iovec iov[10];
    unsigned char syslog[1024];

    /*host = gethostent();
    if(NULL == host)
    {
        printf("error to get the hostent %d \n",errno);
        exit(1);
    }*/

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    //sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        printf("error to create the socket, error %d\n",errno);
        exit(1);
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port   = htons(SPORT);
    if((argc != 4) && (argc != 5) && (argc != 3))
    {
        usage();
        return 1;
    }
    /*it's color*/
    if(atoi(argv[2]) == 1)
    {
        syslog[0] = (0x01<<4)|0x01;
        if(atoi(argv[3]))
        {
            /*enable the color*/
            if(argc != 5)
            {
                usage();
                return 1;
            }
            syslog[1] = 0x01;
            memcpy(syslog+2,argv[4],strlen(argv[4]));
            counter = strlen(argv[4]) + 2;
        }
        else
        {
            syslog[1] = 0x03;
            if(argc != 4)
            {
                usage();
                return 1;
            }
            counter = 2;
        }
    }
    else if( atoi(argv[2]) == 3)
    {
        syslog[0] = (0x03<<4)|0x01;
        if(argc != 4)
        {
            usage();
            return 1;
        }
        if(atoi(argv[3])<0 || atoi(argv[3])>8)
        {
            usage();
            return 1;
        }
        syslog[1] = atoi(argv[3]);
        counter = 2;
    }
    else if( atoi(argv[2]) == 7)
    {        
        syslog[0] = (0x07<<4)|0x01;
        if(argc != 3)
        {
            usage();
            return 1;
        }
        syslog[1] = atoi(argv[2]);

        /*copy the path of the policy */
        //strncpy(syslog+2,argv[3],1024);
        //counter = 2 + strlen(argv[3]);
        counter = 2;
    }
    else if( atoi(argv[2]) == 8)
    {
        mysigval.sival_int = atoi(argv[3]);
        if(sigqueue(atoi(argv[1]), SIGRTMIN+8, mysigval) == -1)
        {
            printf("error to send the signal to the client app\n");
            return 1;
        }
        else
        {
            return 1;
        }
    }
    else
    {
        usage();
        return 1;
    }    
    inet_aton(argv[1],&serv_addr.sin_addr.s_addr);
	printf("%02x \n",serv_addr.sin_addr.s_addr);
    //iserv_addr.sin_addr   = *((struct in_addr*)host->h_addr);
    bzero(&(serv_addr.sin_zero),8);
    idebug = 4*1024*1024;
    setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&idebug,4);
    setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&idebug,4);

    if(connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(struct sockaddr))== -1)
    {
        printf("error to call the connect %d\n",errno);
        exit(1);
    }
    if(-1 == sendto(sockfd,syslog,counter,0,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr)))
    {
            perror("error to send");
            printf("counter %d error to send the traffic %d \n",ipos,errno);
            exit(1);
    }
    return 1;
}

